home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / ClutFade 1.3.2 / Pascal / clut_fade 1.3.1 Pascal Notes next >
Text File  |  1996-03-06  |  5KB  |  141 lines

  1. CLUT Fade 1.3 - Pascal Version Notes
  2.  
  3. Hi everyone. Yesterday I took it upon me to convert the ugly 
  4. C cryptography into something that resembles code. Since it was
  5. not pretty to begin with and I did a straight port, don't 
  6. look here for shining examples of elegant coding. It's ugly.
  7. It's dirty. It's baaad. It works.
  8.  
  9. The Pascal code is reasonable readable (in contrast to the
  10. C original) and works as good as the C version. The original 
  11. was written by Jonas Englund, Mark Womack (markwomack@aol.com) 
  12. and Macneil Shonle (macneils@aol.com), so any kudos for the 
  13. routines are due to them. I just happened to be around and did 
  14. the port.
  15.  
  16.  
  17. Porting Notes:
  18. ==============
  19. The pascal conversion was written for THINK Pascal, so you
  20. might run into some problems when porting it to MW Pascal
  21. (very probably with the 'CYCLE' statement). Other than that
  22. the routines should port all right (if you also port the
  23. demo program, be sure to change @thePort to qd.thePort).
  24.  
  25. Below you will find some notes on a few more peculiar 
  26. points of interests inside the fade port and how they
  27. came about. You may easily skip the rest of this document
  28. if all you want is the fade functionality without knowledge
  29. about it's inner workings.
  30. However, since I'm a Pascal advocat, the comments you find 
  31. here focus only on the aspect of porting the C version to
  32. Pascal. You won't find an explanation of how the fade routines
  33. actually work, just how they were ported. To discover the 
  34. former, read the code. Remember: 'Code doesn't have to be
  35. commented -- it's obvious' (plus, 'if it was hard to write
  36. then it should be hard to read').
  37.  
  38.  
  39. Elegance Factor: Low
  40. --------------------
  41. The Pascal code seems to be unreasonable convoluted. This is
  42. not because I'm a bad programmer (which may be true :-)
  43. but because it is a direct 1:1 port of the original C code.
  44. For example, most of the 
  45.  
  46.     i:=0; 
  47.     WHILE expr(i) DO BEGIN 
  48.         ... 
  49.         i := i + 1; 
  50.     END; 
  51.  
  52. loops would have been much better coded through a FOR block.
  53. Actually, all of these loops originally *where* FOR loops in 
  54. C. But in C a loop can have conditions as boundaries; so for
  55. the sake of clarity, I chose to implement them through WHILE.
  56.  
  57.  
  58. Eat my shorts!
  59. --------------
  60. If you compare the C and Pascal sources side to side, two things
  61. are immediately appearent: 
  62.     
  63.     1. Pascal looks much better although/because it's more eloquent
  64.     
  65.     2. These mysterious BitANDs!
  66.  
  67. Number one is obvious and the reason you are reading this in the 
  68. first place. Number two has to do with one of the most annoying
  69. shortcomings of the Pascal semantic: no provisions for unsigned 
  70. integers. This is usually not a problem since you can always use
  71. longints when dealing with numbers larger than 32766. There is one
  72. exception, though: RGB colors range from 0-65535 ($0000-$FFFF) in
  73. Red, Green and Blue and their field type is integer. To set a
  74. value to max you can pass $FFFF. When you read it, though, it 
  75. returns a '-1' (correctly, since this is two's complement integer
  76. arithmetic). This is bad because now you can't use the rgb
  77. value as a CONSISTENT upper/lower boundary for fading (for 
  78. example, when fading from Gray ($A000) to Black ($0000)).
  79. Typecasting integer to longint does not do the trick because
  80. Pascal cleverly extends the last bit (#15) to bits 16 to 31
  81. to preserve the value (thus a -1 integer won't turn to +65535
  82. longint). This is the reason why you will find those mysterious
  83.  
  84.     color := BitAND(color,$0000FFFF);
  85.     
  86. statements. They simply undo the results of the extension of
  87. bit 15. After the BitAND a -1 integer is a 65535 longint. Now
  88. we can add/subtract and compare to heart's content. To avoid 
  89. the same problem when going back from longint to integer, I 
  90. simply used the LOWORD function to copy the lower 16 bits 
  91. into integer.
  92.  
  93. C does not have this problem since it provides a variable of
  94. type SHORT that is actually an unsigned integer. This is about
  95. the only time where I saw C at an actual advantage. OTOH, Nikki
  96. must have recognized this minor flaw when he designed MODULA-2
  97. because -- lo and behold -- it sports a CARDINAL variable type.
  98.  
  99.  
  100. Order reversed
  101. --------------
  102. C and Pascal have radically different approaches to programming.
  103. While in C you just go ahead and program whatever enters your
  104. mind you must pause and think first in Pascal. To call a procedure
  105. it must have been previously defined. Not so in C. Therefore
  106. you find the explicit main program in C at the beginning while
  107. it's at the very end in Pascal. This forced me to either alter
  108. the sequence of functions or use FORWARD declarations. Of both
  109. options, the former is more elegant but destroys the 1:1 
  110. relationship of C and Pascal sources (bad for editing). Thus,
  111. I chose the latter in the fade routines themselves, while I 
  112. happily rearranged the procedures in the not-so-critical 
  113. demo sources.
  114.  
  115.  
  116.  
  117. Parting Notes
  118. =============
  119. I hope you can use the fade routines. I found them to be very
  120. nice and bow to the original authors for a fine piece of work.
  121. Any comments/suggestions for improvements should be directed
  122. to Mark Womack (markwomack@aol.com) or Macneil Shonle 
  123. (macneils@aol.com).
  124.  
  125. If you have any questions regarding the Pascal version, email 
  126. me at cfranz@home.malg.imp.com
  127.  
  128.  
  129. Cheers,
  130. Christian
  131.  
  132. Zürich, 05-MAR-96
  133.  
  134.  
  135.  
  136. "Ankh-Morpork has a technical democracy: On Man, One Vote.
  137. In Ankh-Morpork, the Patrician is that Man. His rule is not so
  138. much a reign of terror, but the occasional shower."
  139.  
  140. Terry Pratchett - Discworld
  141.